package commands;

import IA.*;
import java.util.*;

public class CmdGetAverageCookDeliveryTime extends Command {

    public CmdGetAverageCookDeliveryTime(InformationAggregator IA) {
        super(IA);
    }
    
    @Override
    
    //return average time customers were waiting for food for the last 20 customers
    
    public String execute() {
        int numTimes = 0;
        int sum = 0;
        for(Iterator<Service> itr = ia.getServiceCollection().getCompletedServiceIterator(); itr.hasNext() && numTimes <= 20; numTimes++) {
            Service s = itr.next();
            sum += s.getFoodWaitingTime();
        }
        if(numTimes == 0) return "0.00";
        else {
            java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
            double d = (double)sum/numTimes;
            nf.setMaximumFractionDigits(2); nf.setMinimumFractionDigits(2);
            return nf.format(d);
        }
    }
}
